using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DACobranzas
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DACobranzasInsert
        /// <summary>
        /// Inserta una Cobranza en la tabla Cobranzas 
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cobranzas"></param>
        public void DACobranzasInsert(string Connection, Cobranzas cobranza)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd0 = DataBase.GetStoredProcCommand("ProximoID");

            try
            {
                using (cmd0)
                {
                    DataBase.AddInParameter(cmd0, "@tabla", DbType.String, "Cobranzas");
                    DataBase.AddInParameter(cmd0, "@campo", DbType.String, "Id_Cobranzas");
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd0);
                    while (Odbreader.Read())
                    {
                        cobranza.Id_Cobranzas = Convert.ToInt64(Odbreader["ID_Id_Cobranzas"]);
                    }
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }


            DbCommand cmd = DataBase.GetStoredProcCommand("Cobranzas_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Cobranzas", DbType.Int64, cobranza.Id_Cobranzas);
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, cobranza.Sucursal);
                    DataBase.AddInParameter(cmd, "@Id_Ingreso", DbType.Int64, cobranza.Id_Ingreso);
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cobranza.Id_Ventas);
                    DataBase.AddInParameter(cmd, "@Id_Caja", DbType.Int64, cobranza.Id_Caja);
                    DataBase.AddInParameter(cmd, "@Prefijo", DbType.String, cobranza.Prefijo);
                    DataBase.AddInParameter(cmd, "@Factura", DbType.Int64, cobranza.Factura);
                    DataBase.AddInParameter(cmd, "@PFMes", DbType.Int16, cobranza.PFMes);
                    DataBase.AddInParameter(cmd, "@PFAnio", DbType.Int16, cobranza.PFAnio);
                    DataBase.AddInParameter(cmd, "@Id_Cliente", DbType.Int64, cobranza.Id_Cliente);
                    DataBase.AddInParameter(cmd, "@Cliente", DbType.String, cobranza.Cliente);
                    DataBase.AddInParameter(cmd, "@FechaCaja", DbType.DateTime, cobranza.FechaCaja);
                    DataBase.AddInParameter(cmd, "@FechaCob", DbType.DateTime, cobranza.FechaCob);
                    DataBase.AddInParameter(cmd, "@FechaFac", DbType.DateTime, cobranza.FechaFac);
                    DataBase.AddInParameter(cmd, "@Id_Estado", DbType.Int16, cobranza.Id_Estado);
                    DataBase.AddInParameter(cmd, "@Estado", DbType.String, cobranza.Estado);
                    DataBase.AddInParameter(cmd, "@SaldoCliente", DbType.Decimal, cobranza.SaldoCliente);
                    DataBase.AddInParameter(cmd, "@Monto", DbType.Decimal, cobranza.Monto);
                    DataBase.AddInParameter(cmd, "@Pago", DbType.Decimal, cobranza.Pago);
                    DataBase.AddInParameter(cmd, "@CtaCte", DbType.Decimal, cobranza.CtaCte);
                    DataBase.AddInParameter(cmd, "@Total", DbType.Decimal, cobranza.Total);
                    DataBase.AddInParameter(cmd, "@Obs", DbType.String, cobranza.Obs);
                    DataBase.AddInParameter(cmd, "@Id_Usuario", DbType.Int16, cobranza.Id_Usuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACobranzasGetAll
        public List<Cobranzas> DACobranzasGetAll(string Connection, Cobranzas cobDesde, Cobranzas cobHasta)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cobranzas_GetAll");

                List<Cobranzas> listaCobranzas = new List<Cobranzas>();
                using (cmd)
                {

                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, cobDesde.Sucursal);
                    DataBase.AddInParameter(cmd, "@FechaD", DbType.DateTime, cobDesde.FechaCob);
                    DataBase.AddInParameter(cmd, "@FechaH", DbType.DateTime, cobHasta.FechaCob);
                   

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Cobranzas oCobranza = new Cobranzas();

                        oCobranza.Id_Cobranzas = Convert.ToInt64(Odbreader["Id_Cobranzas"]);
                        oCobranza.Sucursal = Convert.ToInt16(Odbreader["Sucursal"]);
                        oCobranza.Id_Ingreso = Convert.ToInt64(Odbreader["Id_Ingreso"]);
                        oCobranza.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCobranza.Id_Caja = Convert.ToInt64(Odbreader["Id_Caja"]);
                        oCobranza.Prefijo = (string)Odbreader["Prefijo"];
                        oCobranza.Factura = Convert.ToInt32(Odbreader["Factura"]);
                        oCobranza.PFMes = Convert.ToInt16(Odbreader["PFMes"]);
                        oCobranza.PFAnio = Convert.ToInt16(Odbreader["PFAnio"]);
                        oCobranza.Id_Cliente = Convert.ToInt64(Odbreader["Id_Cliente"]);
                        oCobranza.Cliente = (string)Odbreader["Cliente"];
                        oCobranza.FechaCaja = (DateTime)Odbreader["FechaCaja"];
                        oCobranza.FechaCob = (DateTime)Odbreader["FechaCob"];
                        oCobranza.FechaFac = (DateTime)Odbreader["FechaFac"];
                        oCobranza.Id_Estado = (int)Odbreader["Id_Estado"];
                        oCobranza.Estado = (string)Odbreader["Estado"];
                        oCobranza.SaldoCliente = (Decimal)Odbreader["SaldoCliente"];
                        oCobranza.Monto = (Decimal)Odbreader["Monto"];
                        oCobranza.Pago = (Decimal)Odbreader["Pago"];
                        oCobranza.CtaCte = (Decimal)Odbreader["CtaCte"];
                        oCobranza.Total = (Decimal)Odbreader["Total"];
                        oCobranza.Obs = (string)Odbreader["Obs"];
                        oCobranza.Id_Usuario = (int)Odbreader["Id_Usuario"];

                        listaCobranzas.Add(oCobranza);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaCobranzas;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACobranzasGetAllByCliente
        public List<Cobranzas> DACobranzasGetAllByCliente(string Connection, Cobranzas cobDesde, Cobranzas cobHasta)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cobranzas_GetAllByCliente");

                List<Cobranzas> listaCobranzas = new List<Cobranzas>();
                using (cmd)
                {

                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, cobDesde.Sucursal);
                    DataBase.AddInParameter(cmd, "@FechaD", DbType.DateTime, cobDesde.FechaCob);
                    DataBase.AddInParameter(cmd, "@FechaH", DbType.DateTime, cobHasta.FechaCob);
                    DataBase.AddInParameter(cmd, "@Id_Cliente", DbType.Int64, cobDesde.Id_Cliente);


                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Cobranzas oCobranza = new Cobranzas();

                        oCobranza.Id_Cobranzas = Convert.ToInt64(Odbreader["Id_Cobranzas"]);
                        oCobranza.Sucursal = Convert.ToInt16(Odbreader["Sucursal"]);
                        oCobranza.Id_Ingreso = Convert.ToInt64(Odbreader["Id_Ingreso"]);
                        oCobranza.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCobranza.Id_Caja = Convert.ToInt64(Odbreader["Id_Caja"]);
                        oCobranza.Prefijo = (string)Odbreader["Prefijo"];
                        oCobranza.Factura = Convert.ToInt32(Odbreader["Factura"]);
                        oCobranza.PFMes = Convert.ToInt16(Odbreader["PFMes"]);
                        oCobranza.PFAnio = Convert.ToInt16(Odbreader["PFAnio"]);
                        oCobranza.Id_Cliente = Convert.ToInt64(Odbreader["Id_Cliente"]);
                        oCobranza.Cliente = (string)Odbreader["Cliente"];
                        oCobranza.FechaCaja = (DateTime)Odbreader["FechaCaja"];
                        oCobranza.FechaCob = (DateTime)Odbreader["FechaCob"];
                        oCobranza.FechaFac = (DateTime)Odbreader["FechaFac"];
                        oCobranza.Id_Estado = (int)Odbreader["Id_Estado"];
                        oCobranza.Estado = (string)Odbreader["Estado"];
                        oCobranza.SaldoCliente = (Decimal)Odbreader["SaldoCliente"];
                        oCobranza.Monto = (Decimal)Odbreader["Monto"];
                        oCobranza.Pago = (Decimal)Odbreader["Pago"];
                        oCobranza.CtaCte = (Decimal)Odbreader["CtaCte"];
                        oCobranza.Total = (Decimal)Odbreader["Total"];
                        oCobranza.Obs = (string)Odbreader["Obs"];
                        oCobranza.Id_Usuario = (int)Odbreader["Id_Usuario"];

                        listaCobranzas.Add(oCobranza);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaCobranzas;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACobranzasGetAllByVenta
        public List<Cobranzas> DACobranzasGetAllByVenta(string Connection, Cobranzas cobranza)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cobranzas_GetAllByVenta");

                List<Cobranzas> listaCobranzas = new List<Cobranzas>();
                using (cmd)
                {

                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, cobranza.Sucursal);
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cobranza.Id_Ventas);


                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Cobranzas oCobranza = new Cobranzas();

                        oCobranza.Id_Cobranzas = Convert.ToInt64(Odbreader["Id_Cobranzas"]);
                        oCobranza.Sucursal = Convert.ToInt16(Odbreader["Sucursal"]);
                        oCobranza.Id_Ingreso = Convert.ToInt64(Odbreader["Id_Ingreso"]);
                        oCobranza.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCobranza.Id_Caja = Convert.ToInt64(Odbreader["Id_Caja"]);
                        oCobranza.Prefijo = (string)Odbreader["Prefijo"];
                        oCobranza.Factura = Convert.ToInt32(Odbreader["Factura"]);
                        oCobranza.PFMes = Convert.ToInt16(Odbreader["PFMes"]);
                        oCobranza.PFAnio = Convert.ToInt16(Odbreader["PFAnio"]);
                        oCobranza.Id_Cliente = Convert.ToInt64(Odbreader["Id_Cliente"]);
                        oCobranza.Cliente = (string)Odbreader["Cliente"];
                        oCobranza.FechaCaja = (DateTime)Odbreader["FechaCaja"];
                        oCobranza.FechaCob = (DateTime)Odbreader["FechaCob"];
                        oCobranza.FechaFac = (DateTime)Odbreader["FechaFac"];
                        oCobranza.Id_Estado = (int)Odbreader["Id_Estado"];
                        oCobranza.Estado = (string)Odbreader["Estado"];
                        oCobranza.SaldoCliente = (Decimal)Odbreader["SaldoCliente"];
                        oCobranza.Monto = (Decimal)Odbreader["Monto"];
                        oCobranza.Pago = (Decimal)Odbreader["Pago"];
                        oCobranza.CtaCte = (Decimal)Odbreader["CtaCte"];
                        oCobranza.Total = (Decimal)Odbreader["Total"];
                        oCobranza.Obs = (string)Odbreader["Obs"];
                        oCobranza.Id_Usuario = (int)Odbreader["Id_Usuario"];

                        listaCobranzas.Add(oCobranza);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaCobranzas;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region RptCobranzas
        public List<Cobranzas> RptCobranzas(List<Cobranzas> listaCobranzas)
        {
            try
            {
                return listaCobranzas;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}
